package com.sinosoft.sm.user.manager;


import java.util.List;

import com.sinosoft.sm.system.common.exception.ObjectIsNotExistedException;
import com.sinosoft.sm.system.common.utils.StringUtil;
import com.sinosoft.sm.user.dao.UserEntityMapper;
import com.sinosoft.sm.user.entity.UserEntity;
import com.sinosoft.sm.user.manager.api.UserManager;

/**
 * 用户信息管理层
 * @author leichao
 *
 */
public class UserManagerImpl implements UserManager {

	private UserEntityMapper userEntityMapper;
	
	public void setUserEntityMapper(UserEntityMapper userEntityMapper) {
		this.userEntityMapper = userEntityMapper;
	}


	/**
	 * 根据用户名获取用户信息
	 */
	public UserEntity getUserByUsername(String username){
		return userEntityMapper.getUserByUsername(username);
	}
	
	/**
	 * 更新用户密码
	 * 参数为空则返回false
	 * 用户信息不存在抛出异常消息“UserManagerImpl:updateUserPassword调用失败，用户信息不存在”
	 */
	public boolean updateUserPassword(String username, String password)
			throws ObjectIsNotExistedException {
		if( StringUtil.isBlank(username) || StringUtil.isBlank(password)) {
			return false;
		}
		if(userEntityMapper.getUserByUsername(username) == null) {
			throw new ObjectIsNotExistedException("UserManagerImpl:updateUserPassword调用失败，用户信息不存在");
		}
		userEntityMapper.updateUserPassword(username, password);
		return true;
	}

	/**
	 * 根据条件获取用户信息
	 */
	public List<UserEntity> getUsers(UserEntity entity) {
		return userEntityMapper.getUsers(entity);
	}

	/**
	 * 添加用户信息
	 */
	public void addUser(UserEntity userEntity) {
		userEntityMapper.addUser(userEntity);
	}

	/**
	 * 更新用户状态
	 */
	public void updateUserState(String username, String state) {
		userEntityMapper.updateUserStateByUsername(state, username);
	}
}
